﻿using Brc_PaaS.Common.Model.Basic;
using StackExchange.Redis;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace Brc_PaaS.Core.Redis
{
    public class RedisConfig
    {

        private RedisServerEntity redisConfiguraton { get; }
       
        public RedisConfig(RedisServerEntity redisServerEntity)
        {
            redisConfiguraton = redisServerEntity;
        }      
        private readonly object Locker = new object();
        private ConnectionMultiplexer _instance;


        /// <summary>
        /// 单例获取
        /// </summary>
        public ConnectionMultiplexer Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (Locker)
                    {
                        if (_instance == null || !_instance.IsConnected)
                        {
                            try
                            {
                                _instance = GetManager();
                            }
                            catch (Exception ex)
                            {                               
                                throw new Exception(ex.Message);
                            }
                        }
                    }
                }
                return _instance;
            }     
        }        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="connectionString"></param>
        /// <returns></returns>
        private ConnectionMultiplexer GetManager(string connectionString = null)
        {
            connectionString = connectionString ?? redisConfiguraton.ServerAddress;

            var options = ConfigurationOptions.Parse(connectionString);
            options.AbortOnConnectFail = false;
            options.Password = redisConfiguraton.Password;
            options.ConnectTimeout = redisConfiguraton.ConnectTimeout;
            options.ClientName = redisConfiguraton.Name;
            options.DefaultDatabase = redisConfiguraton.InitDB;
            var connect = ConnectionMultiplexer.Connect(options);

            //注册如下事件
            connect.ConnectionFailed += MuxerConnectionFailed;
            connect.ConnectionRestored += MuxerConnectionRestored;
            connect.ErrorMessage += MuxerErrorMessage;
            connect.ConfigurationChanged += MuxerConfigurationChanged;
            connect.HashSlotMoved += MuxerHashSlotMoved;
            connect.InternalError += MuxerInternalError;
            return connect;
        }


        #region 事件
        /// <summary>
        /// 配置更改时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MuxerConfigurationChanged(object sender, EndPointEventArgs e)
        {

        }

        /// <summary>
        /// 发生错误时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void MuxerErrorMessage(object sender, RedisErrorEventArgs e)
        {

        }

        /// <summary>
        /// 重新建立连接之前的错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MuxerConnectionRestored(object sender, ConnectionFailedEventArgs e)
        {

        }

        /// <summary>
        /// 连接失败 ， 如果重新连接成功你将不会收到这个通知
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MuxerConnectionFailed(object sender, ConnectionFailedEventArgs e)
        {

        }

        /// <summary>
        /// 更改集群
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MuxerHashSlotMoved(object sender, HashSlotMovedEventArgs e)
        {

        }

        /// <summary>
        /// redis内部错误
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void MuxerInternalError(object sender, InternalErrorEventArgs e)
        {

        }

        #endregion 事件
    }
}
